import mongoose, { Document, Schema } from 'mongoose';

export interface ILessonPlanVersion extends Document {
  lessonPlanId: mongoose.Types.ObjectId;
  version: number;
  title: string;
  content: string;
  subject: string;
  grade: string;
  courseType: string;
  tags: string[];
  changeLog?: string;
  createdBy: mongoose.Types.ObjectId;
  createdAt: Date;
}

const lessonPlanVersionSchema = new Schema<ILessonPlanVersion>({
  lessonPlanId: {
    type: Schema.Types.ObjectId,
    ref: 'LessonPlan',
    required: true,
  },
  version: {
    type: Number,
    required: true,
  },
  title: {
    type: String,
    required: true,
    trim: true,
    maxlength: 200,
  },
  content: {
    type: String,
    required: true,
  },
  subject: {
    type: String,
    required: true,
    trim: true,
  },
  grade: {
    type: String,
    required: true,
    trim: true,
  },
  courseType: {
    type: String,
    required: true,
    trim: true,
  },
  tags: [{
    type: String,
    trim: true,
  }],
  changeLog: {
    type: String,
    trim: true,
    maxlength: 500,
  },
  createdBy: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
  },
}, {
  timestamps: { createdAt: true, updatedAt: false },
});

// 创建复合索引
lessonPlanVersionSchema.index({ lessonPlanId: 1, version: 1 }, { unique: true });
lessonPlanVersionSchema.index({ lessonPlanId: 1, createdAt: -1 });

export const LessonPlanVersion = mongoose.model<ILessonPlanVersion>('LessonPlanVersion', lessonPlanVersionSchema);